define(["jquery", "bootstrap", "backend", "table", "form"], function (
  $,
  undefined,
  Backend,
  Table,
  Form
) {
  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: "buser/log/index" + location.search,
          add_url: "buser/log/add",
          edit_url: "buser/log/edit",
          del_url: "buser/log/del",
          multi_url: "buser/log/multi",
          import_url: "buser/log/import",
          table: "buser_log",
          restnum_url: "admin/buser/resterrnum",
        },
      });

      var table = $("#table");

      // 初始化表格
      table.bootstrapTable({
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: "id",
        sortName: "id",
        columns: [
          [
            { checkbox: true },
            { field: "id", title: __("Id") },
            { field: "idcard", title: __("Idcard"), operate: "LIKE" },
            { field: "ip", title: __("Ip"), operate: "LIKE" },
            { field: "errornum", title: __("Errornum") },
            {
              field: "createtime",
              title: __("Createtime"),
              operate: "RANGE",
              addclass: "datetimerange",
              autocomplete: false,
              formatter: Table.api.formatter.datetime,
            },
            {
              field: "operate",
              title: __("Operate"),
              table: table,
              events: Table.api.events.operate,
              buttons: [
                {
                  name: "restnum",
                  text: __("重置次数"),
                  title: __("重置次数"),
                  classname: "btn btn-xs btn-warning  btn-ajax",
                  icon: "fa fa-unlock-alt",
                  url: "buser/log/resterrnum",
                  confirm: "确认重置吗？",
                  success: function (data, ret) {
                    Layer.alert(
                      ret.msg,
                      { icon: 6, title: "提示" },
                      function (index) {
                        //在这里执行你想要的操作
                        window.location.reload(); //跳转到指定页面
                      }
                    );

                    //如果需要阻止成功提示，则必须使用return false;
                    return false;
                  },
                  error: function (data, ret) {
                    console.log(data, ret);
                    Layer.alert(ret.msg);
                    return false;
                  },
                },
              ],

              formatter: Table.api.formatter.operate,
            },
          ],
        ],
      });

      // 为表格绑定事件
      Table.api.bindevent(table);
    },
    restnum: function () {
      Controller.api.bindevent();
    },
    add: function () {
      Controller.api.bindevent();
    },
    edit: function () {
      Controller.api.bindevent();
    },
    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
      },
    },
  };
  return Controller;
});
